//
//  UITextField+Extension.swift
//  CommonTool
//
//  Created by tenroadshow on 12.1.22.
//

import UIKit

public extension UITextField {
    
    func inputRestrictions(shouldChangeTextIn range: NSRange,
                           replacementText text: String,
                           maxCharacters: Int,
                           regex: String?) -> Bool {
        guard !text.isEmpty else {  return true }
        
        guard let oldContent = self.text else { return false }
        
        if let _ = self.markedTextRange {
             // 有高亮
            if range.length == 0 {
                // 联想中
                return oldContent.count + 1 <= maxCharacters
            } else {
                // 正则的判断
                if let weakRegex = regex, !NNRegexTool.isMatch(text, pattern: weakRegex) {
                    return false
                }
                
                // 联想选中键盘
                let allContent = oldContent.sub(to: range.location) + text
                if allContent.count > maxCharacters  {
                    let newContent = allContent.sub(to: maxCharacters)
                    self.text = newContent
                    return false
                }
            }
        } else {
            guard !text.isNineKeyBoard() else {
                return true
            }
            // 正则的判断
            if let weakRegex = regex, !NNRegexTool.isMatch(text, pattern: weakRegex) {
                return false
            }
            // 2、如果数字大于指定位数，不能输入
            guard oldContent.count + text.count <= maxCharacters else {
                return false
            }
        }
        return true
    }
}


public extension UITextView {
    
    func inputRestrictions(shouldChangeTextIn range: NSRange,
                           replacementText text: String,
                           maxCharacters: Int,
                           ignoreChars: [String] = ["\r\r", "\n"],
                           regex: String?) -> Bool {
        guard !text.isEmpty else {  return true }
        
        guard let oldContent = self.text else { return false }
        
        if let _ = self.markedTextRange {
             // 有高亮
            if range.length == 0 {
                // 联想中
                return oldContent.count + 1 <= maxCharacters
            } else {
                // 正则的判断
                if let weakRegex = regex, !NNRegexTool.isMatch(text, pattern: weakRegex) {
                    return false
                }
                
                // 联想选中键盘
                let allContent = oldContent.sub(to: range.location) + text
                if allContent.count > maxCharacters  {
                    let newContent = allContent.sub(to: maxCharacters)
                    self.text = newContent
                    return false
                }
            }
        } else {
            guard !text.isNineKeyBoard() else {
                return true
            }
            // 忽略
            if !ignoreChars.isEmpty, ignoreChars.contains(text) {
                return true
            }
            // 正则的判断
            if let weakRegex = regex, !NNRegexTool.isMatch(text, pattern: weakRegex) {
                return false
            }
            // 2、如果数字大于指定位数，不能输入
            guard oldContent.count + text.count <= maxCharacters else {
                return false
            }
        }
        return true
    }
}



public struct NNRegexTool {
    public static func isMatch(_ input: String, pattern: String) -> Bool {
        guard let regex: NSRegularExpression = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
            return false
        }
        let matches = regex.matches(in: input,
                               options: [],
                                 range: NSMakeRange(0, input.utf16.count))
        return matches.count > 0
    }
}


